#include "EventData.hpp"
// #include "common.h"
#include <stdio.h>


// auto logger = spd::stdout_logger_mt("logger", true);

DataGenerator::DataGenerator() {
	PulseDataInit();
}
DataGenerator::~DataGenerator() {

}
void DataGenerator::getrandomNumber(PulseData & mPulseData) {
   uint16_t count = EVENTCOUNT;

	mPulseData.pEventData = (EventRawdata *)malloc(count * sizeof(EventRawdata));
	EventRawdata * pEventData = mPulseData.pEventData;
   for( int i=0; i<EVENTCOUNT; ++i) {
   	#if 0
   	pEventData[i].event_data = eventGen();
   	pEventData[i].pos = posGen(); 
   	#else
   	memset(&(pEventData[i]), 0x00, sizeof(EventRawdata));
   	pEventData[i].event_data = 0x1111111111111111;
   	#endif
   }

   for (int i =0; i< count; i++) {
   	// logger->error("evendata [{}] = {}", i, mPulseData.pEventData[i].event_data);
   }

}
void DataGenerator::EventRawdataInit(PulseData & mPulseData) {
	getrandomNumber(mPulseData);
	// console->warn("EventRawdataInit :{}", mPulseData.eventCount);
	// logger->warn("EventRawdataInit before :{}", mPulseData.eventCount);
	// mPulseData.eventCount += 1;
}

void DataGenerator::PulseDataInit() {
	#if 0
	mPulseData = PulseData();
	memset(&(mPulseData.head0), 0x55, sizeof(mPulseData.head0));
	memset(&(mPulseData.head1), 0x55, sizeof(mPulseData.head1));
	mPulseData.res = 0x00;
	mPulseData.strength = 0x00;
	mPulseData.ID = 0;
	mPulseData.RUN = 0;
	mPulseData.time_local0 = TIME_LOCAL_C;
	mPulseData.time_local1 = TIME_LOCAL_C;
	mPulseData.time_t0_l = TIME_LOCAL_C;
	mPulseData.time_t0_h = TIME_LOCAL_C;

	// mPulseData.eventCount = 100;
	EventRawdataInit(mPulseData);

	// logger->warn("EventRawdataInit after :{}", mPulseData.eventCount);
	// memset(&(mPulseData.crc32), 0x55, sizeof(mPulseData.crc32));
	mPulseData.crc32 = 0x00;
	mPulseData.length = 0x00;
	memset(&(mPulseData.end0), 0xaa, sizeof(mPulseData.end0));
	memset(&(mPulseData.end1), 0xaa, sizeof(mPulseData.end1));
	#endif

}
PulseDataRecieve::~PulseDataRecieve() {
	free(pPulseData->pEventData);
	delete pPulseData;
	}
PulseDataRecieve::PulseDataRecieve(char *pointer, int size) {
	// memcpy(&mPulseData, )
	//printf("PulseDataRecieve construction\n");
	pPulseData = new PulseData();
	memcpy(&(pPulseData->head0), pointer, 8);			//0
	memcpy(&(pPulseData->head1), pointer+8, 8); 		//0+8
	memcpy(&(pPulseData->eventCount), pointer+16, 4); 			//0+8+8
	memcpy(&(pPulseData->strength), pointer+20, 4); 	//0+8+8 +4
	memcpy(&(pPulseData->ID), pointer+24, 8); 			//0+8+8 +4+4
	memcpy(&(pPulseData->RUN), pointer+32, 8); 			//0+8+8+4+4+8
	memcpy(&(pPulseData->time_local0), pointer+40, 8); 	//0+8+8+4+4+8+8
	memcpy(&(pPulseData->time_local1), pointer+48, 8); 	//0+8+8+4+4+8+8+8
	memcpy(&(pPulseData->time_t0_l), pointer+56, 8); 	//0+8+8+4+4+8+8+8+8
	memcpy(&(pPulseData->time_t0_h), pointer+64, 8); 	//0+8+8+4+4+8+8+8+8+8

	int mEventDatasLength =  pPulseData->eventCount * sizeof(EventRawdata);
	eventDataCount = pPulseData->eventCount;
	pPulseData->pEventData = (EventRawdata *)malloc(mEventDatasLength);
	memcpy((char *)pPulseData->pEventData, pointer+72, mEventDatasLength);
	memcpy(&(pPulseData->crc32), pointer+72+mEventDatasLength, 4); 	//0+8+8+4+4+8+8+8+8+8
	memcpy(&(pPulseData->res), pointer+76+mEventDatasLength, 4); 	//0+8+8+4+4+8+8+8+8+8
	memcpy(&(pPulseData->end0), pointer+80+mEventDatasLength, 8); 	//0+8+8+4+4+8+8+8+8+8
	memcpy(&(pPulseData->end1), pointer+88+mEventDatasLength, 8); 	//0+8+8+4+4+8+8+8+8+8

}
void PulseDataRecieve::print() {
    /*
	printf("head0 is :%lx\n", pPulseData->head0);
	printf("head1 is :%lx\n", pPulseData->head1);
	printf("ID is :%lx\n", pPulseData->ID);
	printf("end0 is :%lx\n", pPulseData->end0);
	printf("end1 is:%lx\n", pPulseData->end1);
     */
	printf("time_local0 is:%lx\n", pPulseData->time_local0);
	printf("time_local1 is:%lx\n", pPulseData->time_local1);
    /*
	printf("time_t0_l is:%lx\n", pPulseData->time_t0_l);
	printf("time_t0_h is:%lx\n", pPulseData->time_t0_h);
     */
	
	#if 1
	for(int i=0; i<eventDataCount; i++) {
		printf("event[%d]:event_data is:%lx\n",i,  pPulseData->pEventData[i].event_data);
		
		}
		#endif
	
}

uint64_t PulseDataRecieve::getPulseDataID() {
	return pPulseData->ID;
	}
PulseData * PulseDataRecieve::getPulseDataPointer(){
	return pPulseData;
	}

uint64_t PulseDataRecieve::getEventDataCount() {
	return eventDataCount;
	}

